package com.it.gw.modules.station.adapter

import EquimentVo
import EquimentVoList
import android.annotation.SuppressLint
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.it.gw.databinding.ItemEquimentBinding


class EquimentAdapter(
    var context: Context,
    var leftListener: ((EquimentVo?) -> Unit)? = null,
    var rigjtListener: ((EquimentVo?) -> Unit)? = null,
    var datas: List<EquimentVoList>
) : RecyclerView.Adapter<EquimentAdapter.ViewHolder>() {
    companion object {
        const val TAG = "EquimentAdapter"
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val binding = ItemEquimentBinding.inflate(LayoutInflater.from(parent.context))
        return ViewHolder(binding)
    }


    override fun onBindViewHolder(holder: ViewHolder, @SuppressLint("RecyclerView") position: Int) {
        val data = datas[position]
        Log.d(TAG, "onBindViewHolder: $data")
        holder.binding.equipment1.text = data.list?.get(0)?.equipment
        if( data.list?.get(1)?.equipment!=null){
            holder.binding.equipment2.text = data.list?.get(1)?.equipment!!.split("_")[0]
        }


        holder.binding.equipment1.setOnClickListener {
            leftListener?.invoke(data.list?.get(0))
        }
        holder.binding.equipment2.setOnClickListener {

            Log.d(TAG, "onBindViewHolder: rigjtListener ${data.list?.get(1)}")
            rigjtListener?.invoke(data.list?.get(1))
        }


    }

    override fun getItemCount() = datas.size

    class ViewHolder(val binding: ItemEquimentBinding) :
        RecyclerView.ViewHolder(binding.root)
}